<template>
    <yh-dialog
        :visible.sync="visibleTemp"
        v-bind="$attrs"
        title="附件上传内容填写"
        :modal="false"
        :close-on-click-modal="false"
        drag
        zoomable
        width="60vw"
        @close="handleCloseDialog"
    >
        <div class="dialog-main">
            <el-form :inline="true" :model="indexDetails" :label-position="right" label-width="100px">
                <el-row class="form-row" v-if="['监管类', '制度解读类'].includes(MenuName)">
                    <el-col :span="12">
                        <el-form-item label="版本号">
                            <span style="position: absolute; left: -10px; color: red">*</span>
                            <el-input v-model="indexDetails.versionNumber" size="mini" style="width: 105%" maxlength="20"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="是否有效">
                            <el-select v-model="indexDetails.valid" placeholder="请选择" size="mini">
                                <el-option label="有效" value="1"></el-option>
                                <el-option label="无效" value="0"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row class="form-row" v-else-if="MenuName == '合同类'">
                    <el-col :span="12">
                        <el-form-item label="合同名称">
                            <span style="position: absolute; left: -10px; color: red">*</span>
                            <el-input v-model="indexDetails.versionNumber" size="mini" style="width: 105%" maxlength="20"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="合同类型">
                            <span style="position: absolute; left: -10px; color: red">*</span>
                            <el-select v-model="indexDetails.productType" placeholder="请选择" size="mini" clearable>
                                <el-option label="资产管理合同" value="1" key="1"></el-option>
                                <el-option label="补充协议" value="2" key="2"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row class="form-row">
                    <el-col :span="12">
                        <el-form-item label="发布日期" v-if="['监管类', '制度解读类'].includes(MenuName)">
                            <el-date-picker
                                v-model="indexDetails.issueDate"
                                placeholder="选择日期"
                                size="mini"
                                style="width: 94%"
                                format="yyyy-MM-dd"
                                value-format="yyyyMMdd"
                            >
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item label="签署日期" v-else-if="MenuName == '合同类'">
                            <el-date-picker
                                v-model="indexDetails.issueDate"
                                placeholder="选择日期"
                                size="mini"
                                style="width: 94%"
                                format="yyyy-MM-dd"
                                value-format="yyyyMMdd"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="生效日期">
                            <el-date-picker
                                v-model="indexDetails.effectiveDate"
                                placeholder="选择日期"
                                size="mini"
                                style="width: 94%"
                                format="yyyy-MM-dd"
                                value-format="yyyyMMdd"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row class="form-row">
                    <el-col :span="12">
                        <el-form-item label="迁移条文">
                            <el-select
                                v-model="indexDetails.moveIndexArticleCode"
                                filterable
                                clearable
                                placeholder="请选择"
                                size="mini"
                                @change="changeMovedIndexSource"
                            >
                                <el-option
                                    :label="item.articleName"
                                    :value="item.articleCode"
                                    v-for="item in indexSourceCodeSelect1"
                                    :key="item.articleCode"
                                    >{{ item.articleName }}</el-option
                                >
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="迁移版本" v-if="['监管类', '制度解读类'].includes(MenuName)">
                            <el-select
                                v-model="indexDetails.moveIndexArticleDetailsCode"
                                filterable
                                clearable
                                placeholder="请选择"
                                size="mini"
                            >
                                <el-option
                                    :label="item.versionNumber"
                                    :value="item.articleDeatilsCode"
                                    v-for="item in moveIndexArticleCodeList"
                                    :key="item.articleDeatilsCode"
                                    >{{ item.versionNumber }}</el-option
                                >
                            </el-select>
                        </el-form-item>
                        <el-form-item label="是否有效" v-else-if="MenuName == '合同类'">
                            <el-select v-model="indexDetails.valid" placeholder="请选择" size="mini">
                                <el-option label="有效" value="1"></el-option>
                                <el-option label="无效" value="0"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row class="form-row">
                    <el-col :span="12">
                        <el-form-item label="附件">
                            <el-upload
                                :multiple="true"
                                :action="actionUrl"
                                :headers="headerObj"
                                :with-credentials="true"
                                list-type="text"
                                :show-file-list="false"
                                :on-success="
                                    (res, file, fileList) => {
                                        return uploadFileSuccess(res, file, fileList, '执行标准');
                                    }
                                "
                                :on-error="
                                    (res, file, fileList) => {
                                        return uploadFileError(res, file, fileList, '执行标准');
                                    }
                                "
                            >
                                <el-button size="small" icon="el-icon-plus" style="border: none">添加文件</el-button>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                </el-row>
                <UploadFileShow
                    style="margin-left: 60px"
                    :fileList="fileList1"
                    @emitDownLoadFile="downLoadFileBtn2"
                    @emitConfirmDelFile="confirmDelFile1"
                ></UploadFileShow>
            </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="insertSourceDetails">确 定</el-button>
        </span>
    </yh-dialog>
</template>
<script lang="ts">
import { downLoadFile } from '@/utils/request';
import { Component, Vue, Prop, PropSync, Watch } from 'vue-property-decorator';
import UploadFileShow from '../commom/upload-file-show.vue';

@Component({ name: 'Attachement', components: { UploadFileShow } })
export default class Attachement extends Vue {
    @PropSync('visible') syncVisible: boolean;
    @Prop() sourceDetail: any;
    @Prop() treeCurrentData: any; // 选中树节点
    @Prop() MenuName;
    @Prop() rsaPassWord;
    @Prop() searchParams;
    @Prop() typeItem;

    public visibleTemp: boolean = false;

    indexDetails: any = {};

    fileList1: any[] = [];
    moveIndexSourceIs: boolean = false;

    created() {}

    @Watch('visible', { immediate: true, deep: true })
    onWatchVisible(value) {
        if (value !== this.visibleTemp) {
            this.visibleTemp = value;
            if (!value) {
                this.moveIndexSourceIs = false;
            }
        }
    }

    @Watch('sourceDetail', { immediate: true, deep: true })
    watchIndexDetails(newData) {
        this.indexDetails = newData;
    }

    // 新增来源详情信息
    insertSourceDetails() {
        if (this.indexDetails.versionNumber == '' && ['监管类', '制度解读类'].includes(this.MenuName)) {
            this.yh_message_warn('版本号不可为空');
            return;
        }
        if (this.indexDetails.versionNumber == '' && ['合同类'].includes(this.MenuName)) {
            this.yh_message_warn('合同名称不可为空');
            return;
        }
        if (this.indexDetails.productType == '' && ['合同类'].includes(this.MenuName)) {
            this.yh_message_warn('合同类型不可为空');
            return;
        }
        if (this.indexDetails.moveIndexArticleCode != '' && this.indexDetails.moveIndexArticleDetailsCode == '') {
            this.yh_message_warn('迁移版本号不可为空');
            return;
        }
        this.indexDetails.fileCode = [];
        this.indexDetails.fileName = [];
        this.indexDetails.files = [];
        if (this.fileList1) {
            this.fileList1.forEach(item => {
                this.indexDetails.fileCode.push(item.fileNo);
                this.indexDetails.fileName.push(item.name);
                this.indexDetails.files.push({
                    fileNo: item.fileNo,
                    fileName: item.name,
                });
            });
        }
        let dataList = {};
        dataList = {
            // 来源详情信息
            articleCode: this.indexDetails.articleCode,
            articleDeatilsCode: this.indexDetails.articleDeatilsCode,
            effectiveDate: this.indexDetails.effectiveDate,
            fileCode: this.indexDetails.fileCode,
            fileName: this.indexDetails.fileName,
            issueDate: this.indexDetails.issueDate,
            versionNumber: this.indexDetails.versionNumber,
            valid: this.indexDetails.valid,
            indexType: this.treeCurrentData.indexType,
            moveIndexArticleCode: this.indexDetails.moveIndexArticleCode,
            moveIndexArticleDetailsCode: this.indexDetails.moveIndexArticleDetailsCode,
            indexDirectoryCode: this.treeCurrentData.indexType == '2' ? this.treeCurrentData.productTypes : this.treeCurrentData.parentCode,
            productType: this.indexDetails.productType,
        };
        let flag;

        if (this.typeItem == '新增' && this.treeCurrentData.level != 2) {
            if (this.treeCurrentData.indexType != '2') {
                if (this.indexDetails.moveIndexArticleCode != '' && this.indexDetails.moveIndexArticleDetailsCode != '') {
                    flag = true;
                } else {
                    flag = false;
                }
            } else {
                if (this.indexDetails.moveIndexArticleCode != '') {
                    flag = true;
                } else {
                    flag = false;
                }
            }

            this.$axios.post('/zhiguan/indexArticle/articleDetails/insert', { ...dataList, flag: flag }).then((res: any) => {
                this.yh_message_success('新增成功!');
                this.syncVisible = false;
                this.fileList1 = [];
                this.$emit('emitSource', true, true);
            });
        } else if (this.typeItem == '修改' && this.treeCurrentData.level != 2) {
            let flag;
            if (['1', '3'].includes(this.treeCurrentData.indexType)) {
                if (this.moveIndexSourceIs == false && this.indexDetails.moveIndexArticleCode && this.indexDetails.moveIndexArticleDetailsCode) {
                    flag = true;
                } else {
                    flag = false;
                }
            } else {
                if (this.moveIndexSourceIs == false && this.indexDetails.moveIndexArticleCode) {
                    flag = true;
                } else {
                    flag = false;
                }
            }
            this.$axios
                .post('/zhiguan/indexArticle/articleDetails/update', { ...dataList, flag: flag })
                .then((res: any) => {
                    this.yh_message_success('修改成功!');
                    this.syncVisible = false;
                    this.fileList1 = [];
                    this.moveIndexSourceIs = false;
                    this.$emit('emitSource', true, true);
                })
                .catch(() => {
                    this.moveIndexSourceIs = false;
                    this.syncVisible = false;
                });
        } else if (this.treeCurrentData.level == 2) {
            if (this.indexDetails.moveIndexArticleCode && this.indexDetails.moveIndexArticleDetailsCode) {
                flag = true;
            } else {
                flag = false;
            }
            this.$axios
                .post('/zhiguan/indexArticle/article/details/insert', {
                    indexArticleDetailsPO: { ...dataList, flag },
                    articleName: this.searchParams.label,
                    indexDirectoryCode: this.searchParams.parentId,
                    indexType: this.treeCurrentData.indexType,
                })
                .then((res: any) => {
                    this.yh_message_success('添加成功!');
                    this.syncVisible = false;
                    this.fileList1 = [];
                    this.moveIndexSourceIs = false;
                    this.$emit('emitSource', false, true);
                })
                .catch(() => {
                    this.moveIndexSourceIs = false;
                    this.syncVisible = false;
                });
        }
    }

    // 下载文件
    downLoadFileBtn2(param) {
        // downLoadFile({
        //     url: '/apiAssetFile/downLoad/' + param.fileNo,
        //     method: 'get',
        // });
        console.log('下载文件', param);
        if (param.fileNo == null) {
            this.$message.warning('来源附件为空！');
            return;
        }
        downLoadFile({
            url: '/apiAssetFile/downLoad',
            method: 'post',
            data: { fileId: [param.fileNo], fileName: param.name, passWord: this.rsaPassWord },
        });
    }

    // 删除已上传文件
    confirmDelFile1(param) {
        this.fileList1.forEach((item, index) => {
            if (item.fileNo == param.fileNo) {
                this.fileList1.splice(index, 1);
            }
        });
    }

    /**
     * 关闭dialog事件
     */
    handleCloseDialog(): void {
        this.syncVisible = false;
    }
}
</script>
<style lang="scss" scoped>
</style>
